<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
<script>
function testTextIndent(testContent, characterIndex)
{
    document.body.innerHTML = testContent;
    var editor = document.getElementById('textIndentTest');
    editor.focus();

    var caretRect = textInputController.firstRectForCharacterRange(0, 0);
    var caretXPosition_withoutTextRender = caretRect[0];

    editor.setSelectionRange(0, 0);
    document.execCommand('InsertText', false, 'a');

    caretRect = textInputController.firstRectForCharacterRange(characterIndex, 0);
    var caretXPosition_withTextRender = caretRect[0];
    
    if (caretXPosition_withoutTextRender == caretXPosition_withTextRender)
        return "Success. The caret's x positions of empty&nonempty input field were the same.\n";
    else
        return "Failure. The caret's x position of empty input field was " + caretXPosition_withoutTextRender + ", should have been " + caretXPosition_withTextRender + ".\n"; 
}

function testTextIndentWhenTextAlignsToCenter(textIndent)
{
    document.body.innerHTML = "<input id='textIndentTest' type='text' style='text-align:center;'>";
    var editor = document.getElementById('textIndentTest');
    editor.focus();

    var caretRect = textInputController.firstRectForCharacterRange(0, 0);
    var caretXPosition_withoutTextIndent = caretRect[0];

    editor.blur();
    editor.style.textIndent = textIndent + "px";
    editor.focus();

    caretRect = textInputController.firstRectForCharacterRange(0, 0);
    var caretXPosition_withTextIndent = caretRect[0];

    var desiredCaretXPosition = caretXPosition_withoutTextIndent + textIndent / 2;
    if (desiredCaretXPosition == caretXPosition_withTextIndent)
        return "Success. The caret's x positions was set to desired position when the text-align is center.\n";
    else
        return "Failure. The caret's x position of input field was " + caretXPosition_withTextIndent + ", should have been " + desiredCaretXPosition + ".\n";
}

if (window.testRunner)
    testRunner.dumpAsText();

var output = "";
output += testTextIndent("<input id='textIndentTest' type='text' style='text-indent:30px;'></input>", 0);
output += testTextIndent("<input id='textIndentTest' type='text' style='text-indent:-30px;'>", 0);
output += testTextIndent("<input id='textIndentTest' type='text' style='text-indent:30px;text-align:left'>", 0);
output += testTextIndent("<input id='textIndentTest' type='text' style='text-indent:30px;text-align:right'>", 1);

// When bidi caret affinity is enabled, carets are shown at different visual
// locations. Use the changed test expectations in this case.
var usesBidiAffinity = window.internals && internals.runtimeFlags.bidiCaretAffinityEnabled;
output += testTextIndent("<input id='textIndentTest' type='text' style='text-indent:30px;direction:rtl;'>", usesBidiAffinity ? 1 : 0);
output += testTextIndent("<input id='textIndentTest' type='text' style='text-indent:30px;direction:rtl;text-align:right;'>", usesBidiAffinity ? 1 : 0);

output += testTextIndentWhenTextAlignsToCenter(30);
output += testTextIndentWhenTextAlignsToCenter(-30);
document.body.innerText = output;
</script>
</html>
